/**
 * 评论相关 API 工厂函数
 */

import type { AxiosInstance } from 'axios'

export interface Comment {
  id: string
  content: string
  parentId?: string
  articleId: string
  author: {
    id: string
    name: string
    avatar?: string
    username?: string
  }
  mentionedUsers?: string[]
  likes: number
  isLiked: boolean
  likedUsers?: string[]
  status: 'pending' | 'approved' | 'rejected'
  createdAt: string
  updatedAt: string
  replyCount?: number
}

export interface CreateCommentData {
  articleId: string
  content: string
  parentId?: string
  authorId: string
  mentionedUsers?: string[]
}

export interface CommentListParams {
  articleId: string
  page?: number
  limit?: number
  parentId?: string | null
  sort?: string
  order?: 'asc' | 'desc'
}

export interface CommentListResponse {
  code: number
  message: string
  data: Comment[]
  pagination: {
    page: number
    limit: number
    total: number
    totalPages: number
    hasNext: boolean
    hasPrev: boolean
  }
  timestamp: string
}

export interface CommentResponse {
  code: number
  message: string
  data: Comment
}

export interface CommentStats {
  totalComments: number
  totalReplies: number
  totalInteractions: number
}

export interface User {
  id: string
  name: string
  username: string
  avatar?: string
}

export interface LikeResponse {
  code: number
  message: string
  data: {
    id: string
    likes: number
    isLiked: boolean
  }
}

/**
 * 创建评论 API 对象
 * @param instance Axios 实例
 * @returns 评论 API 方法集合
 */
export function createCommentApi(instance: AxiosInstance) {
  return {
    /**
     * 获取评论列表
     * @param params 查询参数
     * @returns 评论列表响应
     */
    getComments: (params: CommentListParams) =>
      instance.get<CommentListResponse>('/comments', { params }),

    /**
     * 根据ID获取评论详情
     * @param id 评论ID
     * @returns 评论详情
     */
    getCommentById: (id: string) => instance.get<CommentResponse>(`/comments/${id}`),

    /**
     * 创建评论
     * @param data 评论数据
     * @returns 创建的评论
     */
    createComment: (data: CreateCommentData) => instance.post<CommentResponse>('/comments', data),

    /**
     * 更新评论
     * @param id 评论ID
     * @param data 更新数据
     * @returns 更新后的评论
     */
    updateComment: (id: string, data: Partial<CreateCommentData>) =>
      instance.put<CommentResponse>(`/comments/${id}`, data),

    /**
     * 删除评论
     * @param id 评论ID
     * @returns 删除结果
     */
    deleteComment: (id: string) =>
      instance.delete<{
        code: number
        message: string
        data: { id: string; content: string }
      }>(`/comments/${id}`),

    /**
     * 点赞/取消点赞评论
     * @param id 评论ID
     * @param userId 用户ID
     * @returns 点赞结果
     */
    toggleLike: (id: string, userId: string) =>
      instance.post<LikeResponse>(`/comments/${id}/like`, { userId }),

    /**
     * 获取评论的回复列表
     * @param parentId 父评论ID
     * @param params 查询参数
     * @returns 回复列表
     */
    getReplies: (parentId: string, params?: { page?: number; limit?: number }) =>
      instance.get<CommentListResponse>(`/comments/${parentId}/replies`, { params }),

    /**
     * 搜索用户（用于@功能）
     * @param params 搜索参数
     * @returns 用户列表
     */
    searchUsers: (params: { q?: string; limit?: number }) =>
      instance.get<{ code: number; message: string; data: User[] }>('/users/search', { params }),

    /**
     * 获取评论统计信息
     * @param articleId 文章ID
     * @returns 统计信息
     */
    getCommentStats: (articleId: string) =>
      instance.get<{
        code: number
        message: string
        data: CommentStats
      }>(`/articles/${articleId}/comments/stats`),
  }
}

export type CommentApi = ReturnType<typeof createCommentApi>
